package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.AdminInviteActivityPrizeRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.AdminInviteActivityPrizeMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.AdminInviteActivityPrize;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.IAdminInviteActivityPrizeService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 【请填写功能名称】业务层处理
 *
 * @author wang
 * @date 2024-07-18
 */
@Slf4j
@Service
@AllArgsConstructor
public class AdminInviteActivityPrizeServiceImpl implements IAdminInviteActivityPrizeService {

    private final AdminInviteActivityPrizeMapper baseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AdminInviteActivityPrize add(AdminInviteActivityPrizeRequest request) {
        AdminInviteActivityPrize entity = AdminInviteActivityPrize.builder().build();
        BeanUtils.copyProperties(request, entity);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Integer id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Integer> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(AdminInviteActivityPrizeRequest request) {
        AdminInviteActivityPrize entity = AdminInviteActivityPrize.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0 ;
    }

    @Override
    public AdminInviteActivityPrize getById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<AdminInviteActivityPrize> pageList(AdminInviteActivityPrizeRequest request) {
        Page<AdminInviteActivityPrize> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<AdminInviteActivityPrize> query = this.buildWrapper(request);
        IPage<AdminInviteActivityPrize> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<AdminInviteActivityPrize> list(AdminInviteActivityPrizeRequest request) {
        LambdaQueryWrapper<AdminInviteActivityPrize> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<AdminInviteActivityPrize> buildWrapper(AdminInviteActivityPrizeRequest param) {
        LambdaQueryWrapper<AdminInviteActivityPrize> query = new LambdaQueryWrapper<>();
         if (StringUtils.isNotBlank(param.getPrizeName())){
               query.like(AdminInviteActivityPrize::getPrizeName ,param.getPrizeName());
        }
         if (StringUtils.isNotBlank(param.getPrizeImage())){
               query.like(AdminInviteActivityPrize::getPrizeImage ,param.getPrizeImage());
        }
        if (param.getNeedPerson() != null){
               query.eq(AdminInviteActivityPrize::getNeedPerson ,param.getNeedPerson());
        }
         if (StringUtils.isNotBlank(param.getCreateUserId())){
               query.like(AdminInviteActivityPrize::getCreateUserId ,param.getCreateUserId());
        }
         if (StringUtils.isNotBlank(param.getUpdateUserId())){
               query.like(AdminInviteActivityPrize::getUpdateUserId ,param.getUpdateUserId());
        }
        return query;
    }

}
